package com.peanut.controller;

import com.mvc.annotation.*;
import com.peanut.entity.BookTypeFirst;
import com.peanut.entity.JsonResult;
import com.peanut.entity.Page;
import com.peanut.entity.vo.BookVO;
import com.peanut.service.BookService;
import com.spring.annotation.Autowired;
import com.spring.annotation.Controller;

import java.util.List;

@Controller
public class BookController {
	@Autowired
	private BookService service;
	
	@ResponseBody
	@RequestMapping(value = "/book/query.do")
	public JsonResult pageQueryAllByCondition(@RequestBody BookVO vo,
											  @RequestParam(required = true, defaultValue = "1") Integer currentPage,
											  @RequestParam(required = true, defaultValue = "10") Integer pageSize) {
		if (currentPage < 1) {
			currentPage = 1;
		}
		if (pageSize < 1) {
			pageSize = 10;
		}
		
		Page<BookVO> bookVOPage = service.pageQueryAllByCondition(vo, currentPage, pageSize);
		return JsonResult.success(bookVOPage);
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/book/update.do")
	public JsonResult updateBook(@Valid @RequestBody BookVO vo) {
		int i = service.updateById(vo);
		if (i > 0) {
			return JsonResult.success();
		}
		return JsonResult.fail();
	}
	
	
	/**
	 * 自行级联处理了
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/bookType/query.do")
	public JsonResult queryBookType() {
		List<BookTypeFirst> bookTypeFirsts = service.queryAllBookType();
		return JsonResult.success(bookTypeFirsts);
	}
}
